#include "stdafx.h"
#include "DlgMisc.h"
#include "translhextion.h"
#include "mainwindow.h"
#include "constants.h"
#include "globals.h"

// -----------------------------------
// SELECTION
// -----------------------------------
CParseFastDlg::CParseFastDlg(HWND hWnd) : CDialog(hWnd,IDD)
{
}

void CParseFastDlg::OnInitDialog()
{
	GString str;
	m_past_times.SetDlgItem(*this,IDC_EDIT2);
	m_past_skip.SetDlgItem(*this,IDC_EDIT3);

	str.Format(_T("%d"), iPasteTimes);
	m_past_times.SetText(str);
	str.Format(_T("%d"), iPasteSkip);
	m_past_skip.SetText(str);
	
	if( iPasteMode == 1 ) CheckDlgButton (*this, IDC_RADIO1, BST_CHECKED);
	else CheckDlgButton (*this, IDC_RADIO2, BST_CHECKED);
	if( iPasteAsText == TRUE ) CheckDlgButton (*this, IDC_CHECK1, BST_CHECKED);
	//SetFocus();
}

UINT CParseFastDlg::OnOK()
{
	if (IsDlgButtonChecked (*this, IDC_CHECK1) == BST_CHECKED) iPasteAsText = TRUE;
	else iPasteAsText = FALSE;

	if (IsDlgButtonChecked (*this, IDC_RADIO1) == BST_CHECKED) iPasteMode = 1;
	else iPasteMode = 2;

	int i=0;
	GString str;

	str=m_past_times.GetText();
	if (_stscanf(str, _T("%d"), &i) == 0)
	{
		MessageBox(LoadString(IDS_PASTE_TIMES_NAN),LoadString(IDS_TITLE_PASTE_ERROR), MB_OK | MB_ICONERROR);
		return DLGMESS_OPEN;
	}
	if (i<=0)
	{
		MessageBox(LoadString(IDS_PASTE_TIMES_MIN),LoadString(IDS_TITLE_PASTE_ERROR), MB_OK | MB_ICONERROR);
		return DLGMESS_OPEN;
	}
	iPasteTimes = i;

	str=m_past_skip.GetText();
	if (_stscanf(str, _T("%d"), &i) == 0)
	{
		MessageBox(LoadString(IDS_PASTE_SKIP_NAN),LoadString(IDS_TITLE_PASTE_ERROR), MB_OK | MB_ICONERROR);
		return DLGMESS_OPEN;
	}
	iPasteSkip = i;

	if (OpenClipboard (NULL))
	{
		HGLOBAL hClipMemory = GetClipboardData (CF_UNICODETEXT);
		if (hClipMemory != NULL)
		{
			int gsize = GlobalSize (hClipMemory);
			if (gsize > 0)
			{
				TCHAR* pClipMemory = (TCHAR*) GlobalLock (hClipMemory);
				pcPasteText = new TCHAR[gsize];
				memcpy (pcPasteText, pClipMemory, gsize*sizeof(TCHAR));
			}
			GlobalUnlock (hClipMemory);
		}
		CloseClipboard ();
	}

	return DLGMESS_CLOSE;
}

// -----------------------------------
// ABOUT
// -----------------------------------
CAboutDlg::CAboutDlg(HWND hWnd) : CDialog(hWnd,IDD)
{
	EventClick(IDC_BUTTON1,CAboutDlg::OnClickHomepage);
}

void CAboutDlg::OnInitDialog()
{
	GString name;
	name.Format(_T("%s %s (%s)"),sName,sVersion,sSubRelease);

	//SetFocus();
	::SetWindowText(GetDlgItem(IDC_NAME ), name);
	::SetWindowText(GetDlgItem(IDC_AUTHOR), LoadString(IDS_SYSTEM_AUTHOR));
	::SetWindowText(GetDlgItem(IDC_EDIT1 ), sEmail);
	::SetWindowText(GetDlgItem(IDC_BUTTON1 ), sHomepage );
}

void CAboutDlg::OnClickHomepage()
{
	ShellExecute(NULL, _T("open"), sHomepage, NULL, NULL, SW_SHOWNORMAL);
}

// -----------------------------------
// GENERAL SETTINGS
// -----------------------------------
CSettingsDlg::CSettingsDlg(HWND hWnd) : CDialog(hWnd,IDD)
{
	//EventClick(IDC_BUTTON1,CAboutDlg::OnClickHomepage);
}

void CSettingsDlg::OnInitDialog()
{
	GString message;

	message.Format(_T("*\\shell\\Open in %s\\command"),sName);
	HKEY hkeyKEY;
	LONG iResourceKey = RegOpenKeyEx( HKEY_CLASSES_ROOT,
		message,
		0,
		KEY_ALL_ACCESS,
		&hkeyKEY );
	if( iResourceKey == ERROR_SUCCESS )
	{
		CheckDlgButton(GetWnd(),IDC_CHECK6,TRUE);
	}
	RegCloseKey( hkeyKEY );
	::SetWindowText (GetDlgItem (GetWnd(), IDC_EDIT6), sByteOpen.c_str());
	::SetWindowText (GetDlgItem (GetWnd(), IDC_EDIT7), sByteClose.c_str());
	if(bDefByteMark)
	{
		CheckDlgButton(GetWnd(),IDC_CHECK7,TRUE);	
	}
	bContextMenu = IsDlgButtonChecked(GetWnd(),IDC_CHECK6)&1 ? true : false;
	GString cBuf;
	cBuf.Format(_T("%d"), iBPLSetting);
	::SetWindowText (GetDlgItem (GetWnd(), IDC_EDIT1), cBuf);
	cBuf.Format(_T("%d"), iOffsetLengthSetting);
	::SetWindowText (GetDlgItem (GetWnd(), IDC_EDIT2), cBuf);
	CheckDlgButton (GetWnd(), IDC_RADIO5, bAutomaticXAdjust);
	CheckDlgButton (GetWnd(), IDC_RADIO4, !bAutomaticXAdjust);
	//SetFocus(GetWnd());
	if( bUnsignedViewSetting )
		CheckDlgButton( GetWnd(), IDC_RADIO1, BST_CHECKED );
	else
		CheckDlgButton( GetWnd(), IDC_RADIO2, BST_CHECKED );
	
	if( iOpenReadOnlySetting )
		CheckDlgButton( GetWnd(), IDC_CHECK5, BST_CHECKED );
	else
		CheckDlgButton( GetWnd(), IDC_CHECK5, BST_UNCHECKED );
	if( bDiscardChangesSave )
		CheckDlgButton( GetWnd(), IDC_CHECK1, BST_CHECKED );
	else
		CheckDlgButton( GetWnd(), IDC_CHECK1, BST_UNCHECKED );
	::SetWindowText( GetDlgItem( GetWnd(), IDC_EDIT3 ), TxtEditName.c_str() );
	//SetWindowText( GetDlgItem( GetWnd(), IDC_EDIT4 ), sBrowserName.c_str() );

	GString buf2;
	buf2.Format(_T("%d"), iFontSizeSetting);
	::SetWindowText (GetDlgItem (GetWnd(), IDC_EDIT5), buf2);

	// encoding
	switch (iCharacterSetting)
	{
	case ENCODING_BYTE:
		CheckDlgButton (GetWnd(), IDC_BYTE, BST_CHECKED);
		break;
	case ENCODING_WIDE:
		CheckDlgButton (GetWnd(), IDC_WIDE, BST_CHECKED);
		break;
	}
}

UINT CSettingsDlg::OnOK()
{
	GString message;
	TCHAR sbuf[2];

	if(GetDlgItemText(GetWnd(),IDC_EDIT6,sbuf,2))
	{
		sByteOpen = (_tstring) sbuf;
	}
	if(GetDlgItemText(GetWnd(),IDC_EDIT7,sbuf,2))
	{
		sByteClose = (_tstring) sbuf;
	}
	if(IsDlgButtonChecked(GetWnd(),IDC_CHECK7))
	{
		bDefByteMark = true;
	}
	else
	{
		bDefByteMark = false;
	}
	TCHAR cBuf[512];
	int i=-1;
	if (GetDlgItemText (GetWnd(), IDC_EDIT1, cBuf, 512) != 0)
	{
		if (_stscanf (cBuf, _T("%d"), &i) == 0)
		{
			MessageBox (LoadString(IDS_STPERR_BNUM_CANT_PARSE), LoadString(IDS_TITLE_SETTINGS_ERROR), MB_OK | MB_ICONERROR);
			i = -1;
		}
	}
	if (i==-1)
	{
		EndDialog (GetWnd(), 0);
		return 0;
	}
	iBPLSetting = i;
	if (GetDlgItemText (GetWnd(), IDC_EDIT2, cBuf, 512) != 0)
	{
		if (_stscanf (cBuf, _T("%d"), &i) == 0)
		{
			MessageBox (LoadString(IDS_STPERR_OFFSET_CANT_PARSE), LoadString(IDS_TITLE_SETTINGS_ERROR), MB_OK | MB_ICONERROR);
			i = -1;
		}
	}
	if (i==-1)
	{
		EndDialog (GetWnd(), 0);
		return 0;
	}
	iOffsetLengthSetting = i;
	if( GetDlgItemText( GetWnd(), IDC_EDIT3, cBuf, 512 ) != 0 )
	{
		TxtEditName = (_tstring) cBuf;
	}
	else
	{
		MessageBox(LoadString(IDS_NOTICE_NO_TEXT_EDITOR_NAME), LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION );
	}
	//if( GetDlgItemText( GetWnd(), IDC_EDIT4, cBuf, 512 ) != 0 )
	//{
	//	sBrowserName = cBuf;
	//}
	//else
	//{
	//	MessageBox(LoadString(IDS_NOTICE_NO_BROWSER_NAME), LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION );
	//}
	if (iBPLSetting < 1)
		iBPLSetting = 1;
	bAutomaticXAdjust = IsDlgButtonChecked (GetWnd(), IDC_RADIO5)&1 ? true : false;
	if (IsDlgButtonChecked (GetWnd(), IDC_RADIO1))
		bUnsignedViewSetting = TRUE;
	else
		bUnsignedViewSetting = FALSE;

	if( IsDlgButtonChecked( GetWnd(), IDC_CHECK5 ) )
		iOpenReadOnlySetting = TRUE;
	else
		iOpenReadOnlySetting = FALSE;
	if(IsDlgButtonChecked( GetWnd(), IDC_CHECK1 ) )
		bDiscardChangesSave = true;
	else
		bDiscardChangesSave = false;
	int j;
	TCHAR buf2[16];
	if (GetDlgItemText (GetWnd(), IDC_EDIT5, buf2, 16) != 0)
	{
		if (_stscanf (buf2, _T("%d"), &j) == 0)
		{
			MessageBox (LoadString(IDS_STPERR_FNTSIZE_CANT_PARSE), LoadString(IDS_TITLE_SETTINGS_ERROR), MB_OK | MB_ICONERROR);
			j = -1;
		}
	}
	if (j != -1)
		iFontSizeSetting = j;
	// encoding
	if (IsDlgButtonChecked (GetWnd(), IDC_BYTE) == BST_CHECKED) iCharacterSetting = ENCODING_BYTE;
	else iCharacterSetting = ENCODING_WIDE;

	if(bContextMenu == (IsDlgButtonChecked(GetWnd(),IDC_CHECK6)&1))
	{
		;
	}
	else
	{
		message.Format(_T("*\\shell\\Open in %s\\command"),sName);
		HKEY hkeyKEY;
		LONG iResourceKey = RegOpenKeyEx( HKEY_CLASSES_ROOT,
			message,
			0,
			KEY_ALL_ACCESS,
			&hkeyKEY );
		if( iResourceKey == ERROR_SUCCESS )
		{
			message.Format(LoadString(IDS_NOTICE_REMOVE_FROM_SHELL),sName);
			RegCloseKey( hkeyKEY );
			if( MessageBox(message, LoadString(IDS_TITLE_NOTICE), MB_ICONQUESTION | MB_YESNO ) == IDYES )
			{
				GString temp;

				message.Format(_T("*\\shell\\Open in %s\\command"),sName);
				temp.Format(_T("*\\shell\\Open in %s"), sName);
				if( RegDeleteKey( HKEY_CLASSES_ROOT, message ) ==  ERROR_SUCCESS
					&& RegDeleteKey( HKEY_CLASSES_ROOT, temp ) ==  ERROR_SUCCESS )
				{
					message.Format(LoadString(IDS_NOTICE_REMOVED_FROM_SHELL),sName);
					MessageBox(message, LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION );
				}
				else
				{
					message.Format(LoadString(IDS_CMNUERR_CANT_REMOVE_FROM_SHELL),sName);
					MessageBox(message, LoadString(IDS_TITLE_CMENU_ERROR), MB_OK | MB_ICONERROR );
				}
			}
			else
				CheckDlgButton(GetWnd(),IDC_CHECK6,TRUE);
		}
		else
		{
			message.Format(LoadString(IDS_NOTICE_INSTALL_TO_SHELL),sName);
			if( MessageBox(message, LoadString(IDS_TITLE_NOTICE), MB_ICONQUESTION | MB_YESNO ) == IDYES )
			{
				message.Format(_T("*\\shell\\Open in %s\\command"),sName);
				HKEY hkeyKEY;
				LONG iResourceKey = RegCreateKey( HKEY_CLASSES_ROOT, message, &hkeyKEY );
				if( iResourceKey == ERROR_SUCCESS )
				{
					TCHAR exepath[ _MAX_PATH ];
#ifdef UNICODE
					_tcscpy( exepath, _wpgmptr);
#else
					_tcscpy( exepath, _pgmptr );
#endif
					_tstring str1 = exepath;
					str1 += _T(" %1");
					message.Format(LoadString(IDS_CMNU_INSTALL_REMOVE),sName,sName);
					RegSetValue( hkeyKEY, NULL, REG_SZ, str1.c_str(), str1.length() + 1 );
					MessageBox(message, LoadString(IDS_TITLE_CMNU_INSTALL), MB_OK | MB_ICONINFORMATION );
				}
				else
				{
					message.Format(LoadString(IDS_CMNUERR_CANT_INSTALL),sName);
					MessageBox(message, LoadString(IDS_TITLE_CMENU_ERROR), MB_OK | MB_ICONERROR );
				}
			}
			else
			CheckDlgButton(GetWnd(),IDC_CHECK6,FALSE);
		}
	}

	return DLGMESS_CLOSE;
}

// -----------------------------------
// COLOR SETTINGS
// -----------------------------------
typedef struct tagColorData
{
	UINT uID;
	COLORREF color;
	COLORREF preset;
} COLOR_DATA;

static COLOR_DATA ccolor[11]=
{
	IDC_COLOR0,0,RGB(255,0,0),
	IDC_COLOR1,0,RGB(0,0,255),
	IDC_COLOR2,0,RGB(0,0,0),
	IDC_COLOR3,0,RGB(255,255,255),
	IDC_COLOR4,0,RGB(0,0,0),
	IDC_COLOR5,0,RGB(255,255,0),
	IDC_COLOR6,0,RGB(255,150,0),
	IDC_COLOR7,0,RGB(180,0,150),
	IDC_COLOR8,0,RGB(255,0,0),
	IDC_COLOR9,0,RGB(192,192,192),
	IDC_COLOR10,0,RGB(120,0,215)
};

CColorSettingsDlg::CColorSettingsDlg(HWND hWnd) : CDialog(hWnd,IDD)
{
	EventClick(IDC_COLOR0,CColorSettingsDlg::OnClickColor0);
	EventClick(IDC_COLOR1,CColorSettingsDlg::OnClickColor1);
	EventClick(IDC_COLOR2,CColorSettingsDlg::OnClickColor2);
	EventClick(IDC_COLOR3,CColorSettingsDlg::OnClickColor3);
	EventClick(IDC_COLOR4,CColorSettingsDlg::OnClickColor4);
	EventClick(IDC_COLOR5,CColorSettingsDlg::OnClickColor5);
	EventClick(IDC_COLOR6,CColorSettingsDlg::OnClickColor6);
	EventClick(IDC_COLOR7,CColorSettingsDlg::OnClickColor7);
	EventClick(IDC_COLOR8,CColorSettingsDlg::OnClickColor8);
	EventClick(IDC_COLOR9,CColorSettingsDlg::OnClickColor9);
	EventClick(IDC_COLOR10,CColorSettingsDlg::OnClickColor10);
	EventClick(IDC_DEFAULT,CColorSettingsDlg::OnClickDefaultScheme);
}

#define HANDLE_COLOR_CLICK(id)	\
void CColorSettingsDlg::OnClickColor##id##()\
{\
	SetColor(id);\
}\

HANDLE_COLOR_CLICK(0);
HANDLE_COLOR_CLICK(1);
HANDLE_COLOR_CLICK(2);
HANDLE_COLOR_CLICK(3);
HANDLE_COLOR_CLICK(4);
HANDLE_COLOR_CLICK(5);
HANDLE_COLOR_CLICK(6);
HANDLE_COLOR_CLICK(7);
HANDLE_COLOR_CLICK(8);
HANDLE_COLOR_CLICK(9);
HANDLE_COLOR_CLICK(10);

void CColorSettingsDlg::OnClickDefaultScheme()
{
	for(int i=0; i<11; i++)
	{
		ccolor[i].color=ccolor[i].preset;
		m_color[i].SetColor(ccolor[i].preset);
	}
}

void CColorSettingsDlg::SetColor(int id)
{
	CHOOSECOLOR cc;
	COLORREF crCustColors[16]={0};
	for(int i=0; i<11; i++) crCustColors[i]=ccolor[i].preset;
	cc.lStructSize = sizeof (CHOOSECOLOR);
	cc.hwndOwner = *this;
	cc.hInstance = NULL;
	cc.rgbResult = ccolor[id].color;
	cc.lpCustColors = crCustColors;
	cc.Flags = CC_FULLOPEN | CC_RGBINIT;
	cc.lCustData = 0L;
	cc.lpfnHook;
	cc.lpTemplateName = NULL;
	if (ChooseColor (&cc))
	{
		ccolor[id].color=cc.rgbResult;
		m_color[id].SetColor(cc.rgbResult);
	}
}

void CColorSettingsDlg::OnInitDialog()
{
	ccolor[0].color=cTextColorO;
	ccolor[1].color=cTextColorE;
	ccolor[2].color=cTextColor;
	ccolor[3].color=cBkColor;
	ccolor[4].color=cSelTextColor;
	ccolor[5].color=cSelBkColor;
	ccolor[6].color=cChangesColor;
	ccolor[7].color=cHighlightColor;
	ccolor[8].color=cBookmarkColor;
	ccolor[9].color=cSepColor;
	ccolor[10].color=cDTEColor;

	for(int i=0; i<11; i++)
	{
		m_color[i].SetDlgItem(*this,ccolor[i].uID);
		m_color[i].SetColor(ccolor[i].color);
	}

	//SetFocus();
}

UINT CColorSettingsDlg::OnOK()
{
	cTextColorO=ccolor[0].color;
	cTextColorE=ccolor[1].color;
	cTextColor=ccolor[2].color;
	cBkColor=ccolor[3].color;
	cSelTextColor=ccolor[4].color;
	cSelBkColor=ccolor[5].color;
	cChangesColor=ccolor[6].color;
	cHighlightColor=ccolor[7].color;
	cBookmarkColor=ccolor[8].color;
	cSepColor=ccolor[9].color;
	cDTEColor=ccolor[10].color;

	return DLGMESS_CLOSE;
}

// color button wrapper
LRESULT CColorButton:: OnMessage(UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch(iMsg)
	{
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint (hWnd, &ps);
			{
				CMemDC memdc(hdc,*this,false);
				OnPaint(memdc);
			}
			EndPaint(hWnd,&ps);
		}
		break;
	}

	return 0;
}

void CColorButton::OnPaint(CMemDC &memdc)
{
	CRect rect;
	CBrush brush;

	GetClientRect(&rect);
	brush.CreateSolidBrush(color);

	FillRect(memdc,&rect,brush);
}
